Quote regexp in -add-key-based-replacements
authorJustin Burkett <justin@burkett.cc>
Wed, 30 Nov 2016 02:29:32 +0000 (21:29 -0500)
committerJustin Burkett <justin@burkett.cc>
Wed, 30 Nov 2016 02:29:32 +0000 (21:29 -0500)
Should fix #154

which-key-tests.el
which-key.el

index cd055015a190ea87aa37f35f94d096d224575dc2..244992df16a1eeba9fbecc673590b96653a95a4e 100644 (file)
              (which-key--maybe-replace '("C-c C-c" . ""))
              '("C-c C-c" . "complete")))))
 
+(ert-deftest which-key-test--maybe-replace ()
+  "Test `which-key--maybe-replace'. See #154"
+  (let ((which-key-replacement-alist
+         '((("C-c [a-d]" . nil) . ("C-c a" . "c-c a"))
+           (("C-c .+" . nil) . ("C-c *" . "c-c *")))))
+    (which-key-add-key-based-replacements
+      "C-c ." "test ."
+      "C-c \\" "regexp quoting"
+      "C-c [" "bad regexp")
+    (should (equal
+             (which-key--maybe-replace '("C-c g" . "test"))
+             '("C-c *" . "c-c *")))
+    (should (equal
+             (which-key--maybe-replace '("C-c b" . "test"))
+             '("C-c a" . "c-c a")))
+    (should (equal
+             (which-key--maybe-replace '("C-c ." "not test ."))
+             '("C-c ." . "test .")))
+    (should (not
+             (equal
+              (which-key--maybe-replace '("C-c +" "not test ."))
+              '("C-c ." . "test ."))))
+    (should (equal
+             (which-key--maybe-replace '("C-c [" "orig bad regexp"))
+             '("C-c [" . "bad regexp")))
+    (should (equal
+             (which-key--maybe-replace '("C-c \\" "pre quoting"))
+             '("C-c \\" . "regexp quoting")))))
+
 (provide 'which-key-tests)
 ;;; which-key-tests.el ends here
index 496d2cbebd7e0052b3e84bfeb7d573abe33eb32c..7d99fa597a35992ab8c508dd3e06eac063cf0443 100644 (file)
@@ -810,7 +810,7 @@ replacements are added to
   (while key-sequence
     ;; normalize key sequences before adding
     (let ((key-seq (key-description (kbd key-sequence))))
-      (push (cons (cons (format "\\`%s\\'" key-seq) nil)
+      (push (cons (cons (regexp-quote key-seq) nil)
                   (cons nil (or (car-safe replacement) replacement)))
             which-key-replacement-alist)
       (when (consp replacement)
@@ -836,7 +836,7 @@ addition KEY-SEQUENCE REPLACEMENT pairs) to apply."
     (while key-sequence
     ;; normalize key sequences before adding
       (let ((key-seq (key-description (kbd key-sequence))))
-        (push (cons (cons (format "\\`%s\\'" key-seq) nil)
+        (push (cons (cons (regexp-quote key-seq) nil)
                     (cons nil (or (car-safe replacement) replacement)))
               mode-alist)
         (when (consp replacement)